package com.yingli.bluetooth;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Set;

public class MainActivity extends AppCompatActivity {
    //蓝牙适配器
    private BluetoothAdapter bluetoothAdapter;
    //列表视图
    private RecyclerView rvDevices;
    //蓝牙管理器
    private BluetoothManager bluetoothManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rvDevices.findViewById(R.id.r_view);
        PermissionsUtils.getInstance().checkPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, new PermissionsUtils.IPermissionsResult() {
            @Override
            public void passPermissons() {
                initBluetooth();
            }

            @Override
            public void forbitPermissons() {

            }
        });
    }
    public void initBluetooth() {
        bluetoothManager =(BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(receiver,filter);
        Set<BluetoothDevice> devices =bluetoothAdapter.getBondedDevices();
        if (devices.isEmpty()) {
            Toast.makeText(this,"绑定设备为空",Toast.LENGTH_SHORT).show();
        }else{
            AdapterDemo demo = new AdapterDemo(this,devices);
            rvDevices.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
            rvDevices.setAdapter(demo);
        }
        //获取本机的蓝牙地址
//        bluetoothAdapter.getAddress();
        //取消发现蓝牙设备
//        bluetoothAdapter.cancelDiscovery();
        //判断蓝牙是否可用（开启）
//        bluetoothAdapter.isEnabled();
        //开启蓝牙
//        bluetoothAdapter.enable();
        //关闭蓝牙
//        bluetoothAdapter.disable();
        //获取本机蓝牙的名称
//        bluetoothAdapter.getName();
        //获取扫描模式
//        bluetoothAdapter.getScanMode();
        //本机蓝牙是否可被别的设备发现
//        bluetoothAdapter.isDiscovering();
        //开始扫描，并发现设备
        bluetoothAdapter.startDiscovery();
    }
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_NAME);
                String name = device.getName();
                Log.i("deviceName",name+"");
            }
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this,requestCode,permissions,grantResults);
    }
}